package com.gdit.zax.servletjsp;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gdit.zax.Constant;
import com.gdit.zax.dao.ApplyTcDao;
import com.gdit.zax.dao.UserDao;
import com.gdit.zax.entitys.UserInfo;
import com.gdit.zax.factory.DAOFactory;
import com.gdit.zax.utils.DBUtil;
import com.gdit.zax.utils.Repost;
import com.gdit.zax.utils.webUtil;

/**
 * 提交申请
 * @author lenovo
 *
 */
public class PostApplyTc extends HttpServlet {
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String token = webUtil.getCookie(request, "LoginCookie");
		Connection conn = DBUtil.getConnection();
		
		String truename = request.getParameter("truename");
		String identitycard = request.getParameter("identitycard");
		String content = request.getParameter("content");
		
		if (conn==null) {
			Repost.dberror(request, response);
			return;
		}
		
		DAOFactory dao = DAOFactory.init(conn, request, response);
		UserDao udao = dao.getUserDao();
		ApplyTcDao adao = dao.getApplyTcDao();
		UserInfo info = udao.getInfo(Constant.LOGINTYPE_WEB, token);
		
		if (info==null) {
			Repost.nologin(response);
			return;
		}

		if (adao.isapplying(info.getId())) {
			Repost.message("你已提交申请,请等待1-5个工作日", request, response);
			return;
		}
		
		
		if (adao.applytc(info.getId(), truename, identitycard, content)) {
			Repost.message("申请成功!", request, response);
		} else {
			Repost.message("申请失败!", request, response);
		}
		
	}

}
